<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	
	protected $_configSystem;
	protected $_configContactForm;
	
	protected $_con;
	
	
	
	//Layout
	protected $_UsedLayout;
	protected $_ViewResource;

	
	
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
        $this->_ViewResource = $view;
    }
    
    protected function _initDatabase()
    {
    	//temporäre Angabe der Zugangsdaten
    	//DB-Adapter laden
		try{
			$this->_con = Zend_Db::factory('Pdo_Mysql', array(
				    'host'             => 'localhost',
    				'username'         => 'cmsdev',
    				'password'         => 'cmsdev',
    				'dbname'           => 'cmsdev',
		    		'unix_socket'	   => '/var/run/mysqld/mysqld.sock',
		));
				
			//$db = Zend_Db::factory($this->_configSystem->database);
			$this->_con->query("SET NAMES 'utf8'");
		    
		}catch(Exception $e){
		    die('Keine Vebrindung zur Datenbank möglich.');
		}
		
		$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
		$profiler->setEnabled(true);
		
		// Den Profiler an den DB Adapter anfügen
		$this->_con->setProfiler($profiler); 	
        
        $this->_con->setFetchMode(Zend_Db::FETCH_OBJ);
        $this->_con->query('SET NAMES utf8');
        
        
        //Als Default setzen
        Zend_Db_Table_Abstract::setDefaultAdapter($this->_con);
    
        //Daten Cachen
        // definiere die Optionen für das Frontend
			$optionsFront = array(
			    'lifetime'                => 3600,
			    'automatic_serialization' => true
			);
			    
			// definiere die Optionen für das Backend
			$optionsBack = array(
			    'cache_dir' => APPLICATION_PATH.'/data/cache/db/'
			);
        
        
        $cache = Zend_Cache::factory('Core', 'File', $optionsFront, $optionsBack);
        Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
        
        //$config = Sicore_Models_Registry::getConfig(Sicore_Models_Registry::CONFIG_BASE); 
        //$this->_prefix = $config->database->prefix;
    }
    
    protected function _initConfigs()
    {
    	$system = new Zend_Config_Ini(APPLICATION_PATH.'/configs/system/system.ini', APPLICATION_ENV);
    	$this->_configSystem = $system;
    	Zend_Registry::set('configSystem', $system);
    	
    	$system = new Zend_Config_Ini(APPLICATION_PATH.'/configs/system/contact.ini', APPLICATION_ENV);
    	$this->_configContactForm = $system;
    	Zend_Registry::set('configContact', $system);
    }
    
    
    
    protected function _initAutoload()
    {
    	$autoloader = Zend_Loader_Autoloader::getInstance();
    	
    	foreach($this->_configSystem->modules as $modul){
    		$autoloader->registerNamespace($modul.'_');
    		
    		//Scriptpfade für View-Helper
    		$this->_ViewResource->addHelperPath($modul.'/views/helpers',$modul.'_views_helpers');
    	}
    }
    
    protected function _initLayouts()
    {
    	
    	// view and layout setup
        Zend_Layout::startMvc();
    	$view = Zend_Layout::getMvcInstance()->getView();
    	
    	
    	//Layout-Array aus Config lesen
    	$layouts = $this->_configSystem->layouts;
    	
    	
        //Das letze Layout als Standard setzen
    	foreach($layouts as $layout){
    		$path = APPLICATION_PATH."/templates/frontend/".$layout."/";
    		$view->addScriptPath($path);
    		
    		
    		$systemlayout = $layout;
    	}    	
    }
    
    
    
    
	function _initFrontController()
    {
        $frontController = Zend_Controller_Front::getInstance()
        				
        				->setParam('root', APPLICATION_PATH)
        				->setParam('env', APPLICATION_ENV)
        				->setModuleControllerDirectoryName('Controllers')
        				->addModuleDirectory(APPLICATION_PATH . '/application')
        				->setDefaultModule('Appfront')
        				->throwExceptions(true)
        				->setParam('noErrorHandler', false)
        				->setDefaultControllerName('index')
        				->registerPlugin(new Appfront_Controllers_Plugins_Seo());
        				
        				
   
    }
    
	public function run()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->dispatch();
    }
}

